package com.gdust.lease.web.admin.controller.apartment;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdust.lease.common.result.Result;
import com.gdust.lease.model.entity.ApartmentInfo;
import com.gdust.lease.model.enums.ReleaseStatus;
import com.gdust.lease.web.admin.service.ApartmentInfoService;
import com.gdust.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.gdust.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.gdust.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.gdust.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公寓信息管理接口
 */
@Tag(name = "公寓信息管理")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {
    //注入公寓信息表的service
    @Autowired
    private ApartmentInfoService apartmentInfoService;

    /**
     * 保存或更新公寓信息
     * @param apartmentSubmitVo 公寓信息VO,内含：公寓配套id、公寓标签id、公寓杂费值id、公寓图片id
     * @return
     */
    @Operation(summary = "保存或更新公寓信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo) {
        //自定义方法：保存或更新公寓信息
        apartmentInfoService.saveOrUpdateApartment(apartmentSubmitVo);
        return Result.ok();
    }

    /**
     * 根据条件分页查询公寓列表
     *
     * 数据实体为 ApartmentItemVo，包含公寓的基本信息以及房间统计数据（总房间数和空闲房间数）。
     *
     * @param current 当前页
     * @param size 每页记录数
     * @param queryVo  公寓查询条件,内含：省份id、城市id、区域id
     * @return
     */
    @Operation(summary = "根据条件分页查询公寓列表")
    @GetMapping("pageItem")
    public Result<IPage<ApartmentItemVo>> pageItem(@RequestParam long current, @RequestParam long size, ApartmentQueryVo queryVo) {
        // 使用mybatis-plus分页插件创建分页对象，指定当前页和每页记录数
        IPage<ApartmentItemVo> page = new Page<>(current,size);
        //自定义方法：根据查询条件获取分页后的公寓列表
        IPage<ApartmentItemVo> list= apartmentInfoService.pageApartmentItemByQuery(page,queryVo);
        return Result.ok(list);
    }

    /**
     * 根据ID获取公寓详细信息
     *
     * 公寓信息VO包含：图片列表、标签列表、配套列表、杂费列表
     *
     * @param id 传入的公寓id
     * @return
     */
    @Operation(summary = "根据ID获取公寓详细信息")
    @GetMapping("getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id) {
        //自定义方法：根据传入的公寓id获取公寓详细信息
        ApartmentDetailVo apartmentDetailVo = apartmentInfoService.getApartmentDetailById(id);
        return Result.ok(apartmentDetailVo);
    }

    /**
     * 根据id删除公寓信息
     * @param id 传入的公寓id
     * @return
     */
    @Operation(summary = "根据id删除公寓信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        //自定义查询：根据id删除公寓信息
        apartmentInfoService.removeApartmentById(id);
        return Result.ok();
    }

    /**
     * 根据id修改公寓发布状态
     * @param id 公寓id
     * @param status 发布状态值(1:已发布，0未发布)
     * @return
     */
    @Operation(summary = "根据id修改公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id, @RequestParam ReleaseStatus status) {
        LambdaUpdateWrapper<ApartmentInfo> apartmentInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //匹配传入的公寓id是否为公寓id
        apartmentInfoLambdaUpdateWrapper.eq(ApartmentInfo::getId,id);
        //更新发布状态为status的值(1:已发布，0未发布)
        apartmentInfoLambdaUpdateWrapper.set(ApartmentInfo::getIsRelease,status);
        apartmentInfoService.update(apartmentInfoLambdaUpdateWrapper);
        return Result.ok();
    }

    /**
     * 根据区县id查询公寓信息列表
     * @param id 区县id
     * @return
     */
    @Operation(summary = "根据区县id查询公寓信息列表")
    @GetMapping("listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id) {
        LambdaQueryWrapper<ApartmentInfo> apartmentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据传入的区县id与所处区域id进行匹配
        apartmentInfoLambdaQueryWrapper.eq(ApartmentInfo::getDistrictId,id);
        List<ApartmentInfo> list = apartmentInfoService.list(apartmentInfoLambdaQueryWrapper);
        return Result.ok(list);
    }
}














